package admin.ajax;

import helpers.StringHelper;
import helpers.UrlHelper;

import java.io.IOException;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class CheckLinkServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		response.setCharacterEncoding("utf-8");
		String sitemap = (String) request.getParameter("sitemap");
		String reglink = (String) request.getParameter("reglink");
		String regtitle = (String) request.getParameter("regtitle");
		String regcontent = (String) request.getParameter("regcontent");
		String regremove = (String) request.getParameter("regremove");
		String regimage = (String) request.getParameter("regimage");
		String locationimage = (String) request.getParameter("locationimage");
		String type = request.getParameter("type");
		
		sitemap = UrlHelper.fomatSitemap(sitemap);
		
		String link = "";
		String title = "";
		String content = "";
		String image = "";
		
		try {
			Document doc = Jsoup.connect(sitemap).get();
			Elements links = doc.select(reglink);
			if (links.size() > 0) {
				if(!Pattern.matches("^http://[\\w\\W]+$",links.get(0).attr("href")))
				{
					link = "http://"+UrlHelper.fomatDomains(sitemap) + links.get(0).attr("href");
				} else {
					link = links.get(0).attr("href");
				}
				
				//Get image
				try {
					if (locationimage.equalsIgnoreCase("0") && regimage != null && !regimage.isEmpty()) {
						Elements images = doc.select(regimage);
						if (images.size() > 0) {
							if(!Pattern.matches("^http://[\\w\\W]+$",images.get(0).attr("src")))
							{
								image = "http://"+UrlHelper.fomatDomains(sitemap) + images.get(0).attr("src");
							} else {
								image = images.get(0).attr("src");
							}
						}
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
			if (!link.isEmpty()) {
				doc = Jsoup.connect(link).get();
				
				title = doc.select(regtitle).html();
				
				if (!regremove.isEmpty()) {
					doc.select(regremove).remove();
				}
				
				content = doc.select(regcontent).html();
				
				Elements imgs = doc.select(regcontent).select("img");
				for (int i = 0; i < imgs.size(); i++) {
					String src = imgs.get(i).attr("src");
					if(!Pattern.matches("^http://[\\w\\W]+$",src))
					{
						content = content.replace(src, "http://"+UrlHelper.fomatDomains(sitemap) + src);
					}
				}
				
				//Get image
				try {
					if (locationimage.equalsIgnoreCase("1") && regimage != null && !regimage.isEmpty()) {
						Elements images = doc.select(regimage);
						if (images.size() > 0) {
							if(!Pattern.matches("^http://[\\w\\W]+$",images.get(0).attr("src")))
							{
								image = "http://"+UrlHelper.fomatDomains(sitemap) + images.get(0).attr("src");
							} else {
								image = images.get(0).attr("src");
							}
						}
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		request.setAttribute("str", link);
		request.setAttribute("title", title);
		request.setAttribute("content", StringHelper.cleanContent(content));
		request.setAttribute("image", image);
		try {
			request.getRequestDispatcher("/admin/ajax/check_link.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
}
